package com.benshou.bcss.common.validation.config;

import com.benshou.bcss.common.validation.ValidatorWrapper;
import com.benshou.bcss.common.validation.config.props.ValidationProps;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 参数校验配置
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Configuration
public class ValidationConfig {

    /**
     * @return 参数校验器
     */
    @Bean
    public Validator validator(ValidationProps validationProps) {
        try (var factory = Validation.byProvider(HibernateValidator.class).configure()
                .failFast(validationProps.getFailFast())
                .buildValidatorFactory()) {
            return factory.getValidator();
        }
    }

    /**
     * @param validator 参数校验器
     * @return 参数校验器包装器
     */
    @Bean
    public ValidatorWrapper validatorWrapper(Validator validator) {
        return new ValidatorWrapper(validator);
    }
}
